package thread;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author ganbo
 * @date 2022/11/1 22:06
 */
@Slf4j
public class CountdownLatchDemo {

    public static void main(String[] args) throws InterruptedException {
        ExecutorService executor = Executors.newFixedThreadPool(3);
        CountDownLatch countDownLatch = new CountDownLatch(3);

        executor.execute(() -> {
            log.info("begin....");
            sleep(3);
            countDownLatch.countDown();
            log.info("end....");
        });

        executor.execute(() -> {
            log.info("begin....");
            sleep(3);
            countDownLatch.countDown();
            log.info("end....");
        });

        executor.execute(() -> {
            log.info("begin....");
            sleep(3);
            countDownLatch.countDown();
            log.info("end....");
        });


        log.info("waiting...");
        countDownLatch.await();
        log.info("wait end。。。。");
    }


    public static void sleep(int second) {
        try {
            Thread.sleep(second * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


}
